package com.lizhi.parse;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.lizhi.model.ArticleModel;
import com.lizhi.util.Util;

public class ParseArticleInfo {
	Calendar calendar = Calendar.getInstance();
	ExecutorService executorService = Executors.newCachedThreadPool();

	public ParseArticleInfo() {
		calendar.set(2015, 4, 23);
		Util.clearData("https://freebook.firebaseio.com/articles.json");
		parseArticle();
	}

	public void parseArticle() {
		List<ArticleOr> articleOrs = new ArrayList<ParseArticleInfo.ArticleOr>();
		 ArticleOr lizhiwenzhang = new ArticleOr();
		 lizhiwenzhang.type = "lizhiwenzhang";
		 lizhiwenzhang.start = 53;
		 articleOrs.add(lizhiwenzhang);
		
		 ArticleOr lizhigushi = new ArticleOr();
		 lizhigushi.type = "lizhigushi";
		 lizhigushi.start = 30;
		 articleOrs.add(lizhigushi);
		
		 ArticleOr zhichanglizhi = new ArticleOr();
		 zhichanglizhi.type = "zhichanglizhi";
		 zhichanglizhi.start = 21;
		 articleOrs.add(zhichanglizhi);
		
		 ArticleOr lizhichuangye = new ArticleOr();
		 lizhichuangye.type = "lizhichuangye";
		 lizhichuangye.start = 10;
		 articleOrs.add(lizhichuangye);

		ArticleOr lizhirenwu = new ArticleOr();
		lizhirenwu.type = "lizhirenwu";
		lizhirenwu.start = 16;
		articleOrs.add(lizhirenwu);

		List<ArticleOr> finalArticleOrs = new ArrayList<ParseArticleInfo.ArticleOr>();

		for (ArticleOr articleOr : articleOrs) {
			for (int i = 0; i < 10; i++) {
				String url = "http://www.lz13.cn/lizhi/" + articleOr.type + (i == 0 ? "" : "-" + (articleOr.start - i)) + ".html";
				ArticleOr articleOr2 = new ArticleOr();
				articleOr2.type = articleOr.type;
				articleOr2.href = url;
				finalArticleOrs.add(articleOr2);
			}
		}

		List<ArticleModel> urlList = new ArrayList<ArticleModel>();
		try {
			for (ArticleOr articleOr : finalArticleOrs) {
				System.out.println(articleOr.href);
				Document document = Jsoup.connect(articleOr.href).get();
				Elements elements = document.getElementsByClass("PostHead");
				Elements contentElements = document.getElementsByClass("PostContent1");
				for (int i = 0; i < elements.size(); i++) {
					Element element = elements.get(i);
					Element contentElement = contentElements.get(i);
					Elements elements2 = element.getElementsByTag("h3");
					for (Element element2 : elements2) {
						ArticleModel articleModel = new ArticleModel();
						String href = element2.getElementsByTag("a").attr("href");
						String[] temps = href.split("/");
						articleModel.id = temps[temps.length - 1];
						String temp = articleModel.id.substring(0, articleModel.id.indexOf(".html"));
						articleModel.id = temp;
						articleModel.type = articleOr.type;
						articleModel.href = href;
						articleModel.name = element2.text();
						articleModel.brief = contentElement.text();
						urlList.add(articleModel);
						System.out.println(href);
					}
				}
			}

			// for (ArticleModel articleModel : urlList) {
			// System.out.println("name:"+articleModel.name);
			// System.out.println("id:"+articleModel.id);
			// System.out.println("type:"+articleModel.type);
			// System.out.println("href:"+articleModel.href);
			// System.out.println("bref:"+articleModel.brief);
			// System.out.println("-------------next------------");
			//
			// }
			// System.out.println("sum:"+urlList.size());
			int k = 0;
			Random random = new Random();
			while (urlList.size() != 0) {
				int i = random.nextInt(urlList.size());
				System.out.println(i);
				ArticleModel articleModel = urlList.remove(i);
				articleModel.time = String.valueOf(calendar.getTimeInMillis());
				calendar.add(Calendar.MILLISECOND, 1);
				if (k % 8 == 0) {
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}
				k++;
				executorService.execute(new ParseArticle(articleModel));
				Thread.sleep(1000);
			}

		} catch (Exception e) {
			e.printStackTrace();

		}

	}

	public static void main(String[] args) {
		new ParseArticleInfo();
	}

	class ArticleOr {
		public String type;
		public String href;
		public int start;
	}
}
